package org.fuys.coder.domain.user.service.operation.impl;

import org.fuys.coder.common.constants.RedisConstants;
import org.fuys.coder.common.entity.Pair;
import org.fuys.coder.domain.user.repository.IUserOperationRepository;
import org.fuys.coder.infrastructure.util.RedisUtil;
import org.fuys.coder.domain.user.model.req.operation.UserFollowReq;
import org.fuys.coder.domain.user.service.operation.IUserOperationService;
import org.fuys.coder.domain.user.service.repo.IUserService;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * @projectName: fuys-low-coder
 * @package: org.fuys.coder.domain.user.service.operation.impl
 * @className: UserOperationServiceImpl
 * @author: WangJing
 * @description: 用户操作服务实现类
 * @date: 2024/7/28 21:48
 * @version: 1.0
 */

@Service
public class UserOperationServiceImpl implements IUserOperationService {

    @Resource
    private IUserOperationRepository userOperationRepository;

    @Resource
    private IUserService userService;

    @Override
    public boolean followSomeOne(UserFollowReq followReq) {
        String redisKey= RedisConstants.REDIS_FIELD_USER+RedisConstants.REDIS_USE_CONCERN+RedisConstants.REDIS_SPLIT
                +followReq.getUserId();
        List<Object> list = userOperationRepository.getFollowerCache(redisKey);
        List<Pair<Integer,Integer>> concernIds = null;
        //如果redis的数据为空
        if(ObjectUtils.isEmpty(list)){
            //则说明需要到数据库中查询 并存放
            final List<Pair<Integer, Integer>> userNoticed = userService.getUserNoticed(followReq.getUserId());
            if(!ObjectUtils.isEmpty(userNoticed)){
                userNoticed.add(new Pair<>(followReq.getAuthorId(),followReq.getType()));
//                redisUtil.setList(redisKey,userNoticed);
//                //todo 暂时的 应该找出一个可以当redis缓存快要过期的时候自动续期的方法
//                redisUtil.expire(redisKey,7, TimeUnit.DAYS);
                userOperationRepository.setNewFollowerCache(redisKey,userNoticed
                ,7,TimeUnit.DAYS);
                userService.addUserFollower(followReq.getAuthorId(),1);
                return true;
            }
        }else{
            concernIds = list.stream().map(item -> {
                        return ((Pair<Integer,Integer>)item);
                    }).collect(Collectors.toList());
            if(concernIds.contains(new Pair<>(followReq.getUserId(),followReq.getType()))){
                userService.addUserFollower(followReq.getAuthorId(),-1);
                return false;
            }
        }
//        redisUtil.addList(redisKey,followReq.getAuthorId());
        userOperationRepository.addFollowerCache(redisKey,followReq.getAuthorId());
        userService.addUserFollower(followReq.getAuthorId(),1);
        return true;
    }

    @Override
    public void updateFollowData(Integer userId) {
        String redisKey= RedisConstants.REDIS_FIELD_USER+RedisConstants.REDIS_USE_CONCERN+RedisConstants.REDIS_SPLIT
                +userId;
        final List<Object> followerCache = userOperationRepository.getFollowerCache(redisKey);
        if(!ObjectUtils.isEmpty(followerCache)) {
            final List<Pair<Integer, Integer>> collect = followerCache.stream().map(item -> {
                return ((Pair<Integer, Integer>) item);
            }).collect(Collectors.toList());
            userService.updateUserNoticedAuthor(collect,userId);
        }
    }
}
